﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.Entities;

namespace Basic4Net.BusinessRules
{
    public class ListTeacher
    {
        private int _teacherid;
        private string _typename;
        private string _teachcode;
        private string _teachname;
        private int _sexy;
        private DateTime _birthday;

        public int TeacherID
        {
            get{return _teacherid;}
            set{_teacherid = value;}
        }

        public string TypeName
        {
            get{return _typename;}
            set{_typename = value;}
        }

        public string TeachCode
        {
            get{return _teachcode;}
            set{_teachcode = value;}
        }

        public string TeachName
        {
            get{return _teachname;}
            set{_teachname = value;}
        }

        public int Sexy
        {
            get{return _sexy;}
            set{_sexy = value;}
        }

        public DateTime Birthday
        {
            get{return _birthday;}
            set{_birthday = value;}
        }
    }
    public class TeacherType : BaseBR
    {
        public bool Add(t_TeacherType model, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                //效验重名
                int checkName = (from a in db.t_TeacherType
                                 where a.TypeName == model.TypeName
                                 select a).Count();
                if (checkName > 0)
                {
                    msg = "已经存在名称为【" + model.TypeName + "】的分类名称.";
                    result = false;
                }
                if (result)
                {
                    db.t_TeacherType.AddObject(model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败！";
                        result = false;
                    }
                }
                return result;
            }
        }

        public bool Update(t_TeacherType model, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                //效验重名
                int checkName = (from a in db.t_TeacherType
                                 where a.TypeName == model.TypeName && a.RID != model.RID
                                 select a).Count();
                if (checkName > 0)
                {
                    msg = "已经存在名称为【" + model.TypeName + "】的分类名称.";
                    result = false;
                }
                if (result)
                {
                    db.t_TeacherType.FirstOrDefault(t => t.RID == model.RID);
                    db.ApplyCurrentValues("t_TeacherType", model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败";
                        result = false;
                    }
                }
                return result;
            }
        }

        public bool Delete(int rid, ref string msg)
        {
            using (DBEntities db = new DBEntities())
            {
                t_TeacherType model = db.t_TeacherType.FirstOrDefault(t => t.RID == rid);
                db.DeleteObject(model);
                try
                {
                    db.SaveChanges();
                    return true;
                }
                catch
                {
                    msg = "操作失败";
                    return false;
                }
            }
        }

        public IList<t_TeacherType> ListAll()
        {
            using (DBEntities db = new DBEntities())
            {
                 var obj =(from a in db.t_TeacherType
                        select a);
                 return obj.ToList<t_TeacherType>();
            }
        }

        public IList<ListTeacher> ListTeacher(int FType,string TeachCode,string TeachName)
        {
            using (DBEntities db = new DBEntities())
            {
                var obj = (from b in db.TEACHERINFO
                           join c in db.t_TeacherRelation on b.RID equals c.TeacherID into temp1
                           from d in temp1.DefaultIfEmpty()
                           join a in db.t_TeacherType on d.TypeID equals a.RID into temp2
                           from e in temp2.DefaultIfEmpty()
                           where (FType == -1 ? true : (FType == 0 ? d.TypeID == null : d.TypeID == FType))
                           //update by lxy 20100919 修改bug4 
                           && (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.Contains(TeachCode.Trim()))
                           && (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.Contains(TeachName.Trim()))
                           //&& (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.StartsWith(TeachCode.Trim()))
                           //&& (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.StartsWith(TeachName.Trim()))
                           && b.TEACHCODE != "admin"
                           select new ListTeacher
                           {
                               TeacherID = b.RID,
                               TypeName = e.TypeName,
                               TeachCode = b.TEACHCODE,
                               TeachName = b.TEACHNAME,
                               Sexy = (int)b.SEXY != null ? (int)b.SEXY : 0,
                               Birthday = (DateTime)b.BIRTHDAY != null ? (DateTime)b.BIRTHDAY : System.DateTime.MaxValue
                           });
                //if (TeachCode != "")
                //{
                //    obj = obj.Where(t => t.TeachCode.StartsWith(TeachCode));
                //}
                //if (TeachName != "")
                //{
                //    obj = obj.Where(t => t.TeachName.StartsWith(TeachName));
                //}
                return obj.ToList();
            }
        }
    }
}
